﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.TravelLogs;
using VK.Comm;
using VK.Casemeet.Merchants;
using VK.Casemeet.Employees;

namespace VK.Casemeet.Web.Admin.TravelLogs
{
    public partial class TravelLogEdit : System.Web.UI.Page
    {
        public int travelLogID = 0;
        public string content = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="游记日志",Url="/Admin/TravelLogs/TravelLogList.aspx"},
                new MapPath{ Text="编辑日志",Url="#"}
            };
            
            travelLogID = Util.GetQueryInt("TravelLogID");

            if (!IsPostBack)
            {
                TravelLogHelper.BindObjectTypeDDL(ddlObjectType, OptionType.Default);
                LoadData();
            }
        }

        internal void LoadData()
        {
            if (travelLogID != 0)
            {
                TravelLogEntity travelLog = TravelLogBLL.GetTravelLog(travelLogID);
                txtObjectID.Text = travelLog.ObjectID.ToString();
                ddlObjectType.SelectedValue = travelLog.ObjectType.ToString();
                txtTitle.Text = travelLog.Title;
                content = travelLog.Content;
                txtCreated.Text = Util.FormatDateTime(travelLog.Created, DateTimeStyle.Null);
                txtHits.Text = travelLog.Hits.ToString();
                lbIsEnable.Text = PageHelper.GetIsEnableLabel(travelLog.IsEnable);
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            TravelLogEntity travelLog = LoadEntity();
            travelLog.TravelLogID = travelLogID;

            if (TravelLogBLL.UpdateTravelLog(travelLog) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected TravelLogEntity LoadEntity()
        {
            TravelLogEntity travelLog = TravelLogBLL.GetTravelLog(travelLogID);
            travelLog.ObjectID = int.Parse(txtObjectID.Text);
            travelLog.ObjectType = int.Parse(ddlObjectType.SelectedValue);
            travelLog.Title = txtTitle.Text;
            travelLog.Content = Server.HtmlEncode(Util.GetFormString("txtContent"));
            travelLog.Updated = Util.ConverDateTime(txtCreated.Text);
            travelLog.Editor = EmployeeService.GetSessionEmployee().Account; ;

            return travelLog;
        }
    }
}